package com.weilan.ark3.read;

import com.weilan.ark3.buffer.DataArray2;
import com.weilan.ark3.buffer.MsgQueue;
import com.weilan.ark3.buffer.OverallSituation;
import com.weilan.ark3.common.GlobalParam;
import com.weilan.ark3.entity.ControlRtu;
import com.weilan.ark3.serial.SerialPortCom2Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 读取uc数据
 */
@Component

public class COM2ReadData {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SerialPortCom2Service serialPortCom2Service;

    public void ReadRtuData() {
        Logger tcpServer = LoggerFactory.getLogger("TCPServer_Appender");
        while (true) {
            if (MsgQueue.com2QueueSize() > 0) {
                ControlRtu controlRtu = MsgQueue.readFromCom2Queue();
                //起始地址
                Integer address = controlRtu.getAddress();
                Integer offset = controlRtu.getOffset();
                //长度
                Integer len = controlRtu.getLen();
                try {
                    //日志记录发送的报文信息
//                    tcpServer.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                    short[] rtuData = serialPortCom2Service.readRtuData(controlRtu.getSlaveId(), controlRtu.getAddress(), controlRtu.getLen());
                    if (rtuData != null && rtuData.length == len) {
                        System.arraycopy(rtuData, 0, DataArray2.data, address + offset, len);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}


